package com.wty.cmims.controller;

import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.pojo.User;
import com.wty.cmims.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
@Tag(name = "账号管理", description = "社区医疗信息后台管理系统api接口")
public class UserController {

    @Autowired
    private UserService userService;

    // 用户登录
    @PostMapping  ("/login")
    @Operation(summary = "用户登录", description = "用户账号登录数据信息")
    @Parameter(name = "account",description = "账号")
    @Parameter(name = "password",description = "密码")
    @Parameter(name = "role",description = "角色")
    @ApiResponse(description = "处理结果",content = {
            @Content(
                    mediaType="application/json",
                    schema= @Schema(implementation = ResponseResult.class)
            )
    })
    public ResponseResult<Map<String, Object>> userLogin(
            @RequestParam(name = "account") String account,
            @RequestParam(name = "password") String password,
            @RequestParam(name = "role") Integer role
    ){
        return userService.userLogin(account,password,role);
    }

    // 管理员信息修改
    @PutMapping ("/update")
    @Operation(summary = "管理员信息修改", description = "管理员信息修改")
    @Parameter(name = "user",description = "管理员信息")
    @ApiResponse(description = "处理结果",content = {
            @Content(
                    mediaType="application/json",
                    schema= @Schema(implementation = ResponseResult.class)
            )
    })
    public ResponseResult<Map<String,Object>> adminInfoUpdate(@RequestBody Map<String,Object> paramsMap){
        return userService.adminInfoUpdate(paramsMap);
    }

    @PutMapping("/resetPassword")
    @Operation(summary = "密码重置", description = "密码重置")
    @Parameter(name = "user_id",description = "账号ID")
    @ApiResponse(description = "处理结果",content = {
            @Content(
                    mediaType="application/json",
                    schema= @Schema(implementation = ResponseResult.class)
            )
    })
    public ResponseResult<Void> resetPassword(@RequestParam(name = "user_id") Integer userId){
        return userService.resetPassword(userId);
    }

    // 删除账号
    @PutMapping("/deleteUser")
    @Operation(summary = "删除账号", description = "删除账号")
    @Parameter(name = "user_id",description = "账号ID")
    @ApiResponse(description = "处理结果",content = {
            @Content(
                    mediaType="application/json",
                    schema= @Schema(implementation = ResponseResult.class)
            )
    })
    public ResponseResult<Void> deleteUser(@RequestParam(name = "user_id") Integer userId){
        return userService.deleteUser(userId);
    }

    // 管理员修改密码
    @PutMapping("/updatePassword")
    @Operation(summary = "修改密码", description = "修改密码")
    @Parameter(name = "account",description = "账号")
    @Parameter(name = "oldPassword",description = "旧密码")
    @Parameter(name = "newPassword",description = "新密码")
    @ApiResponse(description = "处理结果",content = {
            @Content(
                    mediaType="application/json",
                    schema= @Schema(implementation = ResponseResult.class)
            )
    })
    public ResponseResult<Void> updatePassword(
            @RequestParam(name = "account") String account,
            @RequestParam(name = "oldPassword") String oldPassword,
            @RequestParam(name = "newPassword") String newPassword
    ){
        return userService.updatePassword(account,oldPassword,newPassword);
    }

    // 添加用户
    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户账号注册数据信息")
    @Parameter(name = "name",description = "姓名")
    @Parameter(name = "account",description = "账号")
    @Parameter(name = "password",description = "密码")
    @Parameter(name = "role",description = "角色")
    @ApiResponse(description = "处理结果",content = {
            @Content(
                    mediaType="application/json",
                    schema= @Schema(implementation = ResponseResult.class)
            )
    })
    public ResponseResult<Void> userRegister(
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "account") String account,
            @RequestParam(name = "phone") String phone,
            @RequestParam(name = "title_id", required = false) Integer title_id,
            @RequestParam(name = "department_id", required = false) Integer department_id,
            @RequestParam(name = "institution_id", required = false) Integer institution_id,
            @RequestParam(name = "fee", required = false) Integer fee,
            @RequestParam(name = "role") Integer role
    ){
        return userService.userRegister(name,account,phone,title_id,department_id,institution_id,fee,role);
    }

    // 查询管理员账号
    @PostMapping("/findAdmin")
    @Operation(summary = "查询管理员账号", description = "查询管理员账号")
    @Parameter(name = "paramsMap",description = "参数列表")
    @ApiResponse(description = "处理结果",content = {
            @Content(
                    mediaType="application/json",
                    schema= @Schema(implementation = ResponseResult.class)
            )
    })
    public ResponseResult<Map<String,Object>> findAdmin(@RequestBody Map<String,Object> paramsMap){
        return userService.findAdmin(paramsMap);
    }

    // 修改管理员账号信息
    @PutMapping("/updateAdmin")
    @Operation(summary = "修改管理员账号信息", description = "修改管理员账号信息")
    @Parameter(name = "paramsMap",description = "参数列表")
    @ApiResponse(description = "处理结果",content = {
            @Content(
                    mediaType="application/json",
                    schema= @Schema(implementation = ResponseResult.class)
            )
    })
    public ResponseResult<Map<String,Object>> updateAdmin(@RequestBody Map<String,Object> paramsMap){
        return userService.updateAdmin(paramsMap);
    }

    // 查询医生和管理员账号
    @PostMapping("/findDoctorAndAdmin")
    @Operation(summary = "查询所有管理员账号", description = "查询所有管理员账号")
    @Parameter(name = "paramsMap",description = "参数列表")
    @ApiResponse(description = "处理结果",content = {
            @Content(
                    mediaType="application/json",
                    schema= @Schema(implementation = ResponseResult.class)
            )
    })
    public ResponseResult<Map<String,Object>> findDoctorAndAdmin(@RequestBody Map<String,Object> paramsMap){
        return userService.findDoctorAndAdmin(paramsMap);
    }

    // 账号角色修改
    @PutMapping("/updateRole")
    @Operation(summary = "账号角色修改", description = "账号角色修改")
    @Parameter(name = "user_id",description = "账号ID")
    @Parameter(name = "role",description = "角色")
    @ApiResponse(description = "处理结果",content = {
            @Content(
                    mediaType="application/json",
                    schema= @Schema(implementation = ResponseResult.class)
            )
    })
    public ResponseResult<Void> updateRole(@RequestBody Map<String,Object> paramsMap){
        return userService.updateRole(paramsMap);
    }
}